home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
pascal
/
tpref.exe
/
TPR3A.TXT
< prev
next >
Wrap
Text File
|
1992-10-19
|
60KB
|
1,461 lines
Chapter 3
- Part 1 of 3 parts -
of the
Turbo Pascal Reference
The Turbo Pascal Language
This chapter is part of the Turbo Pascal Reference electronic freeware book (C)
Copyright 1992 by Ed Mitchell. This freeware book contains supplementary
material to Borland Pascal Developer's Guide, published by Que Corporation,
1992. However, Que Corporation has no affiliation with nor responsibility for
the content of this free book. Please see Chapter 1 of the Turbo Pascal
Reference for important information about your right to distribute and use this
material freely. If you find this material of use, I would appreciate your
purchase of one my books, such as the Borland Pascal Developer's Guide or
Secrets of the Borland C++ Masters, Sams Books, 1992. Thank you.
Note: For ease of access, Chapter 3 is continued in TPR3B.TXT and TPR3C.TXT.
The Pascal programming language was originally developed by Niklaus Wirth
as an idealized language for teaching basic concepts of programming. Many of
the concepts and constructions used in Pascal trace their ancestry to the Algol
programming language developed in the early 1960's. By 1968, Wirth had
developed the basic structure of Pascal, but it was not until his publication
of the Pascal User Manual and Report in 1973 that Pascal received wide spread
acceptance.
Since that time, the core structure of Pascal has remained largely the
same, but many signficant enhancements have been made, especially in the
version known as Turbo Pascal. These enhancements include:
Units, for the sharing and reuse of code,
Powerful object oriented programming capabilities,
Extensive library support including graphics, overlays (for managing
large programs), and system-level access for system programming.
Yet, because Turbo Pascal traces its heritage to Pascal's early
development as a tool for teaching programming, Turbo Pascal is probably the
premier programming language for learning modern programming concepts and
practices. And with Borland's significant enhancements, Turbo Pascal has
become the definitive implementation of the Pascal language, providing one of
the most powerful development languages and environments available. The
professional editions of Turbo Pascal and Borland Pascal are every bit as
powerful as C++, plus Borland's Pascal compilers generate fast, compact code,
often much smaller than similar C++ code. Except for Pascal's lack of
templates and overloaded functions, Turbo Pascal has all the features and
capabilities of C++.
This chapter presents an overview of the entire Turbo Pascal language,
except for object oriented programming (OOP) features. OOP may be found in
Chapter 4, "Object Oriented Programming" in the Borland Pascal Developer's
Guide.
Your First Turbo Pascal Program
Common practice is to introduce a new programming language by writing a
simple program that simply displays,
Hello, World!
In Turbo Pascal, such a program look liks this:
program Hello;
begin
Writeln('Hello, World!');
end.
Each Pascal program begins with a statement containing the keyword program and
the program name, shown here as Hello, optional variable declarations (none
shown in this example), the keyword begin, zero or more Pascal statements, and
an end statement to terminate the program. Each statement is terminated with a
semicolon, except the last end statement is terminated in a period.
The statement,
Writeln('Hello, World!');
calls a built-in Pascal routine called Writeln, which has as its single
parameter, the contents of the text to be printed.
All Pascal programs have roughly the same structure as that used by the
Hello program, but with many more options available.
While the Turbo Pascal Reference includes substantial tutorial and
reference information, if you have little or no programming experience, I
suggest you consult an introductory text first.
Programmers who are experienced in Pascal or other programming languages
such as C, QuickBasic or Visual Basic, will find that Turbo Pascal Reference
provides a concise and complete description of the Turbo Pascal language,
libraries, development environment and the powerful Turbo Vision
character-based windowing system.
Pascal Program Structure
Each Pascal program is organized in a standard format, as shown in figure
3.1. Turbo Pascal, unlike other implementations of Pascal, does not require
the strict ordering of declarations suggested by figure 3.1. In Turbo Pascal,
you can, if you wish, place type declarations before the const declarations, or
even add a second var section after you have defined procedures and functions.
You do not need to have each of the declarations sections shown, but only those
that are used by your program. Figure 3.2 displays a "railroad" syntax diagram
illustrating how the declarations may occur in any order. These syntax
diagrams are frequently used to describe the structure of Pascal programs. You
read the diagram by starting, usually, at the upper left corner and tracing
through the drawing, through each keyword or symbol in the language, until you
exit the diagram, usually a the upper right.
Figure 3.1. The structure of Pascal program.
program programname;
uses
unitnames-list;
label
declarations;
const
declarations;
type
declarations;
var
variable declarations;
procedure and function declarations;
begin
Main body of program;
end.
***03tpr02.pcx***
Figure 3.2. A syntax diagram showing how the elements of the Pascal language
declarations may appear in any order.
Pascal Data Types
Data types determine the type of data and the permissible range of values
within each type upon which Pascal can operate. These data types range from a
variety of small and large integer values to several real or floating point
number formats, plus character and string values. The basic data types of
Pascal are shown in Table 3.1.
For reasons of program efficiency, some values may be represented by more
than one type. Consider a numeric value in the range of 0 to 100. Such a
value can be represented by an Integer, Byte, Shortint, Longint or other
standard types. These values vary in the amount of memory storage they
require, and hence, the length of time it takes the CPU to process such data.
For example, the Byte data type uses 8 bits of memory, while the Longint type
takes 32 bits.
Table 3.1 presents the standard predefined Turbo Pascal data types. As
described later in this chapter, you may also create custom data types, as
appropriate.
Table 3.1. The standard Pascal data types.
Data Type Typical Values Size and Description
Boolean True, False 1 byte
A Boolean value holds one of the predefined constants True or False,
where Ord(True) = 1 and Ord(False) = 0.
Byte 0 to +255 1 byte
A Byte value is an integer in the range of 0 to +255 and is
represented in an unsiged 8 bit format.
Shortint -128 to +127 1 byte
A numeric value in the range -128 to +127 is a signed 8-bit value
called a Shortint.
Integer -32768 to +32767 2 bytes
Integers store numeric values in the range of -32768 to +32767 in
signed 16 bit format.
Word 0 to 65535 2 bytes
The Word data type is an unsigned 16 bit value and is used when
numbers larger than standard integers are required, and when negative
values are not needed.
Longint -2147483648 to 2147483647 4 bytes
Longints store signed 32-bit values and are used for storing very
large integer values. They may also be used for high speed fixed
point operations fo